package com.wanmait.movies.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.wanmait.movies.util.DBHelper;
import com.wanmait.movies.vo.Concerned;
import com.wanmait.movies.vo.User;

public class ConcernedDao {

	private ConcernedDao() {}
	private static ConcernedDao concernedDao = new ConcernedDao();
	public static ConcernedDao getConcerned() {
		return concernedDao;
	}
	//获取是否被关注
	public Concerned getIsConcerned( int userId,int reUserId) {
		Connection con=DBHelper.getDBHelper().getconn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select * from concerned where user_id=? and reuser_id=?";	
		try {
			ps=con.prepareStatement(sql);
			
			ps.setInt(1, userId);
			ps.setInt(2, reUserId);
			
			rs=ps.executeQuery();
			
			if(rs.next()) {
				Concerned c= new Concerned();
				c.setId(rs.getInt("id"));
				
				return c;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally 
		{
			DBHelper.getDBHelper().close(rs, ps, con);
		}
		return null;
	}
	   
	public ArrayList<Concerned> getMyConcernsed(int userId, int page){//���������˵�id�����ݱ��˵�id���ҹ�ע����Աid
		ArrayList<Concerned> allconcerned=new ArrayList<Concerned>();
		Connection con=DBHelper.getDBHelper().getconn();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select * from concerned,`user` WHERE user_id=? and reuser_id=`user`.id order by concerned.id desc  limit ?,5";//��ע����Աid������Ա��id
		try {
			ps=con.prepareStatement(sql);
			ps.setInt(1, userId);
			ps.setInt(2, (page-1)*5);
			rs=ps.executeQuery();
			while(rs.next()) {
             User user=new User();
             user.setId(rs.getInt("user.id"));
             user.setUserFaceimage(rs.getString("user_faceimage"));
             user.setUserName(rs.getString("user_name"));
             Concerned concerned=new Concerned();
             concerned.setUserId(user);
             concerned.setId(rs.getInt("concerned.id"));
             
             concerned.setReuserId(user);
             allconcerned.add(concerned);//ȡ���� ��ӵ� ����
			}
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			DBHelper.getDBHelper().close(rs, ps, con);
		}
		
		return allconcerned;//���ؼ���
	}
	  public int getPageMax(int userId) {//���ݴ���������id��Ϊ�����鿴�Ĺ�ע�˼�����
	      Connection con=DBHelper.getDBHelper().getconn();
	 		PreparedStatement ps=null;
	 		ResultSet rs=null;
	 		int max=0;
	 		String sql="select count(id) as m from concerned where user_id=?";
	 		try {
	 			ps=con.prepareStatement(sql);
	 			ps.setInt(1, userId);
	 			rs=ps.executeQuery();
	 			rs.next();
	 			max=rs.getInt("m");
	 		} catch (SQLException e) {
	 			// TODO Auto-generated catch block
	 			e.printStackTrace();
	 		}
	 		finally {
	 			DBHelper.getDBHelper().close(rs, ps, con);
	 			
	 		}
	 		return max;
	     }
	  
	  //取消关注
	    public void ConcernedDelete(int userid,int reuserid) {
	    Connection con=	DBHelper.getDBHelper().getconn();
	    PreparedStatement ps=null;
	    String sql="delete from concerned where user_id=? and reuser_id=?";
	    try {
			ps=con.prepareStatement(sql);
			ps.setInt(1, userid);
			ps.setInt(2, reuserid);
			
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    finally {
	    	DBHelper.getDBHelper().close(null, ps, con);
	    }
	 }
	     
	    public void ConcernedDelete(String ids) {
		    Connection con=	DBHelper.getDBHelper().getconn();
		    PreparedStatement ps=null;
		    String sql="delete from concerned where id in("+ids+")";
		    try {
				ps=con.prepareStatement(sql);
				
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    finally {
		    	DBHelper.getDBHelper().close(null, ps, con);
		    }
		 }
	    
	    //添加关注
	    public void insertConcerned(int userid,int reuserid) {
		    Connection con=	DBHelper.getDBHelper().getconn();
		    PreparedStatement ps=null;
		    String sql="insert into concerned(user_id,reuser_id) values(?,?)";
		    try {
				ps=con.prepareStatement(sql);
				ps.setInt(1, userid);
				ps.setInt(2, reuserid);
				ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    finally {
		    	DBHelper.getDBHelper().close(null, ps, con);
		    }
		   }

}

































